<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2016/9/13
 * Time: 17:14
 */
namespace app\home\controller;
use think\Controller;
use app\common\model\User as UserModel;//为了跟User控制器作为区分
use think\View;//视图类
use think\Request;//请求类
use think\Session;
class Login extends Controller{
    protected $userModel;
    //初始化
    public function _initialize() {
        //多处重复利用 可放在初始化函数里加载
        $this->userModel = new UserModel();
    }

    /**
     * 登录首页
     * @return mixed
     */
    public function index () {
        //实例化 视图类 注意：如果没有继承父类Controller的话 模板常量是不会被解析的
        //继承了Controller了话,此处可以直接$this->fetch() 输出的
        if (Session::get('uid') && Session::get('username')) {
            $this->redirect('home/index/index');
        }
        return View::instance()->fetch('index');
    }

    /**
     * ajax 处理登录
     * @return string
     */
    public function ajax_send() {
        //tp5 系统判断为ajax请求的情况下 系统输出返回数据 会自动调节为json 而不是html输出
        if (Request::instance()->isPost()) {
            //使用tp5 自带验证器
            $data = Request::instance()->post();
            //使用了 验证场景
            $result = $this->validate($data,'User.login');
            if(true !== $result){
                return ['status' => 0,'msg' => $result];
            }
            //通过验证 获取模型数据
            if ($this->userModel->send_login()) {
                return ['status'=>1,'msg' => '登录成功.^_^','url' => url('home/index/index')];
            } else {
                return ['status'=>0,'msg' => $this->userModel->getError()];
            }
        }
    }

    /**
     * 退出登录
     */
    public function loginOut() {
        // 清除session（当前作用域）
        Session::clear();
        // 清除think作用域
        Session::clear('think');
        $this->redirect('home/login/index');
    }

    /**
     * 注册页面
     * @return mixed
     */
    public function register() {
        return View::instance()->fetch('register');
    }

    /**
     * 检测用户名唯一
     */
    public function ajax_check_account() {
        if ($this->request->instance()->isAjax()) {
            $account = $this->request->instance()->post('account');
            if ($this->userModel->checkAccount($account)) {
                return ['ok' => ''];
            } else {
                return ['error' => $this->userModel->getError()];
            }
        }
    }

    /**
     * ajax 处理注册
     */
    public function ajax_register() {
        if ($this->request->instance()->isAjax()) {
            //获取所有数据
            $data = $this->request->instance()->post();
            // 数据验证 取得User表验证器
            $result = $this->validate($data,'User');
            if (true !== $result) {
                return ['error' => $result];
            }
            if ($this->userModel->send_register()) {
                return ['status'=>1, 'msg' => '注册成功.^_^','url' => url('home/index/index')];
            } else {
                return ['status'=>0, 'msg' => $this->userModel->getError()];
            }
        }
    }

    /**
     * ajax 检测验证码
     */
    public function ajax_check_verify() {
        if ($this->request->instance()->isAjax()) {
            $captcha = $this->request->instance()->post('verify');
            //验证
            if(!captcha_check($captcha)){
                return ['error' => '验证码错误'];
            } else {
                return ['ok'=>''];
            }
        }
    }

}